<?PHP

/**
 *	Set up variables to use within the page.
 *
 *		- status_names: inverted array of $mConfig['status'] which
 *		associates a status name to a number. Now we can go from
 *		number to name, just by swapping array key and value.
 *
 *		- row: identifies the row for alternating colors. simply
 *		finds the mod of row and zero to determine even or odd.
 *
 *		- edit_mode: this is for use only if the user is an admin. allows
 *		them to edit each subscriber for the event individually. if
 *		not set, the admin can select multiple subscribers to make
 *		updates en masse.
 *
 *		- event: this is an instance of the event class, and is the
 *		source of all data about the event itself.
 */

$status_names =			array_flip($mConfig['status']);
$row =					0;

(isset($_GET['editmode']) and $_SESSION['mul_permission'] == OFFICER) ?
$edit_mode = 			TRUE :
$edit_mode =			FALSE;

$event =				new event($mul_db, $smf_db, $_GET['eid']);





/**
 * Now we need to get some data about the event.
 *
 *		- getSubscriber: all information about each subscriber.
 *
 *		- getTotals: subscriber statistics for this event.
 *
 *		- userCharacterStatus: sorts the user's characters based
 *		on which are subscribed and those that are not. Subscribed
 *		characters can be updated from this page, non-subscribed
 *		are displayed in a drop down menu so users can also
 *		subscribe from this page.
 *			(user instance comes from index page)
 */

$event->getSubscribers();
$event->getTotals();
$event->userCharacterStatus($user);





/**
 * Print the page header.
 *
 *		- format the date for start and invite time.
 *		- setup the admin link based on edit mode status.
 *			(but only print it if user is an admin)
 *		- the table header is the subscriber table.
 */

$date_starttime =		date('l n/j/y \a\t g:ia', $event->start_time);
$date_invitetime =		date('g:ia', $event->invite_time);

($edit_mode == FALSE) ?
$editmode_link =		'<a href="index.php?page=event&eid='.$event->eid.'&amp;editmode">Admin Mode</a> &middot;' :
$editmode_link =		'<a href="index.php?page=event&eid='.$event->eid.'">User Mode</a> &middot;';


echo '
<div class="event_title">
	<h2>'.$event->location.'</h2>
	<h3>On '.$date_starttime.' ('.$date_invitetime.' invite)</h3>

	<div class="event_controls">',
	($_SESSION['mul_permission'] == OFFICER) ? $editmode_link : '', '
	
	<a href="index.php?page=event&eid='.$event->eid.'">Refresh</a> &middot;
	<a href="index.php">Event List</a>
	</div>
</div>',

($edit_mode == FALSE) ?
'<form action="index.php?page=event&amp;eid='.$event->eid.'" method="post" id="adminform">
<input type="hidden" name="process" value="admin" />
<input type="hidden" name="event_id" value="'.$event->eid.'" />' : '','

<TABLE class="eventinfo" id="subs_table">
<TR class="emphasize">
	<TH><a href="#nogo">Name</a></TH>
	<TH><a href="#nogo">Class</a></TH>
	<TH><a href="#nogo">Role</a></TH>
	<TH><a href="#nogo">Talent</a></TH>
	<TH><a href="#nogo">Status</a></TH>
	<TH><a href="#nogo">Date Subscribed</a></TH>
	<TH>Comments</TH>
	<TH>',

		($edit_mode == FALSE and $_SESSION['mul_permission'] == OFFICER) ?
		'<input type="checkbox" name="checksubs" value="" onclick="checkall()" />' : 'Action','

	</TH>
</TR>';





/**
 * Loop through each subscriber and print a table row.
 *
 *		- is_users_character: boolean value; does the subscriber
 *		for this loop belong to the user? If yes, we make their
 *		character information into a form so they can make updates.
 *
 *		- subscriber_output: array of what we need to output in
 *		each column.
 */

foreach ($event->subscribers as $subscriber)
{
	global $mConfig;

	($_SESSION['mul_permission'] == OFFICER) ?
	$is_users_character =	FALSE :
	$is_users_character = 	in_array($subscriber->char_name, $event->chars_subscribed['character']);

	unset($subscriber_output);
	$subscriber_output =	array();





	/**
	 * Build the columns (part I).
	 *		If we're not in edit mode (applies only to admins) and
	 *		this character doesn't belong to user (applies to members only).
	 */

	if ($edit_mode == FALSE and $is_users_character == FALSE)
	{
		// ----------- HTML Form
		$subscriber_output['form'] = '';
		$subscriber_output['form2'] = '';



		// ----------- Row Color
		switch ($subscriber->status)
		{
			case ATTENDING: 	$subscriber_output['rowcolor'] = 'attending';		break;
			//case AVAILABLE: 	$subscriber_output['rowcolor'] = 'available';		break;
			//case QUEUE: 		$subscriber_output['rowcolor'] = 'queue';			break;
			case UNAVAILABLE: 	$subscriber_output['rowcolor'] = 'unavailable';		break;
			default:			($row % 2 == 0) ?
								$subscriber_output['rowcolor'] = 'even' :
								$subscriber_output['rowcolor'] = 'odd';
								break;
		}



		// ----------- Name
		$subscriber_output['name'] =
			'<a href="http://www.wowarmory.com/character-sheet.xml?r=Kilrogg&n='.$subscriber->char_name.'">
			'.$subscriber->char_name.'</a>';



		// ----------- Class
		$subscriber_output['class'] =
			'<img src="images/class_icons/'.strtolower($subscriber->class).'_icon.gif"
				alt="" width="20" height="20" />';



		// ----------- Role
		$subscriber_output['role'] = $mConfig['roles'][$subscriber->role];



		// ----------- Talent
		$subscriber_output['talent'] =
			'<a href="http://www.wowarmory.com/character-talents.xml?r=Kilrogg&n='.$subscriber->char_name.'">
			'.$subscriber->talent.'</a>';



		// ----------- Status
		$subscriber_output['status'] = ucwords($status_names[$subscriber->status]);



		// ----------- Subscribed Time
		$subscriber_output['subscribe_time'] = date('n/j/y H:i', $subscriber->sub_time);



		// ----------- Comments
		if ($subscriber->comments != '')
		{
			$subscriber_output['comments'] =
				'<img src="images/quote_left.png" alt="Left quote." width="27" height="18" class="left_quote" />'
				.$subscriber->comments.
				'<img src="images/quote_right.png" alt="Right quote." width="27" height="18" class="right_quote" />';
		}
		else
		{
			$subscriber_output['comments'] = $subscriber->comments;
		}



		// ----------- Action
		if ($_SESSION['mul_permission'] == OFFICER)
		{
			$subscriber_output['action'] =
				'<input type="checkbox" name="attendees[]" value="'.$subscriber->sid.'" />';
		}
		else
		{
			$subscriber_output['action'] = '';
		}
	}

	/**
	 * Build the columns (part II).
	 *		We are in edit mode (applies only to admins) which makes all
	 *		subscribers become editable, or this subscriber belongs
	 *		to user (applies to members only).
	 */

	else
	{
		// ----------- HTML Form
		($edit_mode == TRUE) ? $url = '&amp;editmode' : $url = '';

		$subscriber_output['form'] = '
			<form action="index.php?page=event&amp;eid='.$event->eid.$url.'" method="post" id="update_form'.$subscriber->sid.'">
			<input type="hidden" name="process" value="subscribe" />
			<input type="hidden" name="event_id" value="'.$event->eid.'" />
			<input type="hidden" name="is_update" value="1" />
			<input type="hidden" name="user_char" value="'.$subscriber->char_name.'" />';
		$subscriber_output['form2'] = '</form>';



		// ----------- Name
		$subscriber_output['name'] =
			'<a href="http://www.wowarmory.com/character-sheet.xml?r=Kilrogg&n='.$subscriber->char_name.'">
			'.$subscriber->char_name.'</a>';



		// ----------- Class
		$subscriber_output['class'] =
			'<img src="images/class_icons/'.strtolower($subscriber->class).'_icon.gif"
				alt="" width="20" height="20" />';



		// ----------- Role
		$subscriber_output['role'] = '<select name="user_role" id="user_role">'."\r\n";

		foreach ($mConfig['roles'] as $abbr => $role)
		{
			($abbr == $subscriber->role) ?
				$select = ' selected="selected"' :
				$select = '';

			$subscriber_output['role'] .= '<option value="'.$abbr.'"'.$select.'>'.$role.'</option>'."\r\n";
		}

		$subscriber_output['role'] .= '</select>';
		unset($key); unset($status);



		// ----------- Talent
		$subscriber_output['talent'] =
			'<a href="http://www.wowarmory.com/character-talents.xml?r=Kilrogg&n='.$subscriber->char_name.'">
			'.$subscriber->talent.'</a>';



		// ----------- Status
		$subscriber_output['status'] = '<select name="user_status" id="user_status">'."\r\n";

		if ($subscriber->status == ATTENDING)
			$subscriber_output['status'] .= '<option value="'.ATTENDING.'" selected="selected">Attending</option>'."\r\n";

		foreach ($mConfig['status_menu'] as $key => $status)
		{
			($key == $subscriber->status) ?
				$select = ' selected="selected"' :
				$select = '';

			$subscriber_output['status'] .= '<option value="'.$key.'"'.$select.'>'.$status.'</option>'."\r\n";
		}

		$subscriber_output['status'] .= '</select>';
		unset($key); unset($status);



		// ----------- Subscribed Time
		$subscriber_output['subscribe_time'] = date('n/j/y H:i', $subscriber->sub_time);



		// ----------- Comments
		$subscriber_output['comments'] =
			'<textarea name="comment" cols="36" rows="5">'.$subscriber->comments.'</textarea>';



		// ----------- Action
		$subscriber_output['action'] = '<input type="submit" value="Update" />';
	}





	// Print the columns.
	echo '
	<!-- Begin Row -->
	<TR class="'.$subscriber_output['rowcolor'].'">


	<!-- Subscribe Form -->
	'.$subscriber_output['form'].'


	<!-- Character Name -->
	<TD>'.$subscriber_output['name'].'</TD>


	<!-- Character Class -->
	<TD>'.$subscriber_output['class'].'</TD>


	<!-- Subscriber Role -->
	<TD>'.$subscriber_output['role'].'</TD>


	<!-- Character Spec -->
	<TD>'.$subscriber_output['talent'].'</TD>


	<!-- Subscriber Status -->
	<TD>'.$subscriber_output['status'].'</TD>


	<!-- Subscribed Date & Time -->
	<TD>'.$subscriber_output['subscribe_time'].'</TD>


	<!-- User Comments -->
	<TD width="40%"><div class="comments">'.$subscriber_output['comments'].'</div></TD>


	<!-- Actions & Options -->
	<TD>'.$subscriber_output['action'].'</TD>


	<!-- End Row -->
	'.$subscriber_output['form2'].'</TR>';

	$row++;
}





/**
 * End the subscriber table (part I).
 *		If we are not in edit mode, but the user is an admin
 *		they need options to go with all those checkboxes
 *		for each subscriber. Currently admins can mark people
 *		for attendance or remove them completely en masse.
 */

if ($edit_mode == FALSE and $_SESSION['mul_permission'] == OFFICER)
{
	echo '
	<TR class="emphasize">
		<TD colspan="6"></TD>
		<TD>
			<select name="action" id="action">
			<option value="attend" selected="selected">Attending</option>
			<option value="remove">Remove</option>
			</select>&nbsp;&nbsp;<input type="submit" value="Submit" />
		</TD>
		<TD></TD>
	</TR>
</TABLE>

</form>';
}


/**
 * End the subscriber table (part II).
 *		So if the user is a regular member, but not all
 *		of their characters are subscribed to this event,
 *		we need to print another form so they can
 *		subscribe from this page.
 *
 *		Or if we're in edit mode (only for admins) we get 
 *		another form for manually adding people to the event.
 *
 *		- subscribe_form: array of what we need to output in
 *		each column.
 */

elseif (count($event->chars_not_subscribed) > 0 or $edit_mode == TRUE)
{
	$subscribe_form = array();



	if ($edit_mode == TRUE)
	{
		$subscribe_form['characters'] = '<input name="user_char" maxlength="20" value="" />';
	}
	else
	{
		$subscribe_form['characters'] = '<select name="user_char" id="user_char">'."\r\n";

		for ($i = 0; $i < count($event->chars_not_subscribed['character']); $i++)
		{
			$subscribe_form['characters'] .= '<option value='.strtolower($event->chars_not_subscribed['character'][$i]).'>
			'.ucwords($event->chars_not_subscribed['character'][$i]).'</option>'."\r\n";
		}

		$subscribe_form['characters'] .= '</select>'."\r\n";
		unset($char);
	}




	$subscribe_form['roles'] = '
		<select name="user_role" id="user_role">
		<option value="null">Select one:</option>';
	foreach ($mConfig['roles'] as $abbr => $role)
	{
		if ($edit_mode == TRUE)
			$selected = '';
		else
			($event->chars_not_subscribed['role'][0] == $abbr) ? $selected = ' selected = "selected"' : $selected = '';

		$subscribe_form['roles'] .= '<option value="'.$abbr.'"'.$selected.'>'.$role.'</option>'."\r\n";
	}
	$subscribe_form['roles'] .= '</select>'."\r\n";
	unset($abbr); unset($role);




	$subscribe_form['status'] = '
		<select name="user_status" id="user_status">
		<option value="null">Select one:</option>';
	foreach ($mConfig['status_menu'] as $key => $status)
	{
		$subscribe_form['status'] .= '<option value="'.$key.'">'.$status.'</option>'."\r\n";
	}
	$subscribe_form['status'] .= '</select>'."\r\n";
	unset($key); unset($status);





	// Print the columns.
	echo '
	<!-- Begin Row -->
	<TR class="emphasize">


	<!-- Subscribe Form -->
	<form action="index.php?page=event&eid='.$event->eid.'" method="post" id="subscribe_form">
	<input type="hidden" name="process" value="subscribe" />
	<input type="hidden" name="event_id" value="'.$event->eid.'" />
	<input type="hidden" name="is_update" value="0" />


	<!-- Characters Available to Subscribe -->
	<TD colspan="2">'.$subscribe_form['characters'].'</TD>


	<!-- Roles Menu -->
	<TD>'.$subscribe_form['roles'].'</TD>
	<TD></TD>


	<!-- Available Statuses -->
	<TD>'.$subscribe_form['status'].'</TD>


	<!-- Comments -->
	<TD></TD>
	<TD><input name="comment" maxlength="100" size="30" value="" /></TD>


	<!-- Action -->
	<TD><input type="submit" value="Subscribe" /></TD>


	<!-- End Row -->
	</form></TR>
</TABLE>';
}


/**
 * End the subscriber table (part III).
 *		Finally, if the user has no available characters
 *		to subscribe, we just print a blank row.
 */

else
{
	echo '
	<!-- Filler Row -->
	<TR class="emphasize"><TD colspan="8">&nbsp;</TD></TR>
</TABLE>';	
}










/**
 * Tables for event statistics.
 *
 *		We already counted the totals for this event
 *		so now we just need to print it all out.
 */


echo '
<TABLE class="eventdata" id="stats_role">
<TR class="emphasize">
	<TH></TH>
	<TH>MT</TH>
	<TH>OT</TH>
	<TH>Heal</TH>
	<TH>DPS</TH>
	<TH></TH>
	<TH>Total</TH>
	<TH></TH>
	<TH><img src="images/class_icons/druid_icon.gif" width="20" height="20" /></TH>
	<TH><img src="images/class_icons/hunter_icon.gif" width="20" height="20" /></TH>
	<TH><img src="images/class_icons/mage_icon.gif" width="20" height="20" /></TH>
	<TH><img src="images/class_icons/paladin_icon.gif" width="20" height="20" /></TH>
	<TH><img src="images/class_icons/priest_icon.gif" width="20" height="20" /></TH>
	<TH><img src="images/class_icons/rogue_icon.gif" width="20" height="20" /></TH>
	<TH><img src="images/class_icons/shaman_icon.gif" width="20" height="20" /></TH>
	<TH><img src="images/class_icons/warlock_icon.gif" width="20" height="20" /></TH>
	<TH><img src="images/class_icons/warrior_icon.gif" width="20" height="20" /></TH>
</TR>';


foreach ($event->totals as $status => $count)
{
	echo '
	<TR>
		<TD>'.ucwords($status).'</TD>';


	foreach ($mConfig['roles'] as $abbr => $role)
	{
		echo '
			<TD class="'.getStyle($count[$abbr], $event->limits[$abbr]).'">'.
			$count[$abbr].'/'.$event->limits[$abbr].'</TD>';
	}
	unset($abbr); unset($role);


	echo '
		<TD>&nbsp;&nbsp;</TD>
		<TD class="'.getStyle($count['total'], $event->cap).'">'.$count['total'].'/'.$event->cap.'</TD>
		<TD>&nbsp;&nbsp;</TD>
		<TD>'.$count['Druid'].'</TD>
		<TD>'.$count['Hunter'].'</TD>
		<TD>'.$count['Mage'].'</TD>
		<TD>'.$count['Paladin'].'</TD>
		<TD>'.$count['Priest'].'</TD>
		<TD>'.$count['Rogue'].'</TD>
		<TD>'.$count['Shaman'].'</TD>
		<TD>'.$count['Warlock'].'</TD>
		<TD>'.$count['Warrior'].'</TD>
	</TR>';
}

echo '
	<TR class="emphasize">
		<TD colspan="17">&nbsp;</TD>
	</TR>
</TABLE>';
unset($status); unset($count);

?>